// pages/ad/add.js
const app = getApp();
Page({
    data:{
      time1:"嘿嘿，啥子时候",
      images:[],
      urls:[],
  },
  look(e){
    console.log(e);
    let index=e.target.dataset.index   //通过 index 属性拿到当前被选中的图片
    let that=this  //保存 this 指向
    wx.previewImage({  
      current:that.data.images[index],  //获取所要预览图片的链接 
      urls:that.data.images  //上传的全部图片
    })
},
bindTimeChange1: function(e) {
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    time1: e.detail.value
  })
},
del(e){
  var that=this
  var images=that.data.images
  var index=e.currentTarget.dataset.index
  wx.showModal({
      title:'提示',
      content:'确定要删除此图片吗？',
      success:(res)=>{
          if(res.confirm){
              images.splice(index,1)
          }else if(res.calcel){
              return false
          }
          that.setData({
              images
          })
      }
  })
},
upload() {
  var that = this;
  wx.chooseImage({
    count: 9,
    sizeType: ['original', 'compressed'],
    sourceType: ['camera', 'album'],
    success: (res) => {
    let tempFilePath=res.tempFilePaths
      this.setData({
           images:tempFilePath
       })
      wx.showLoading({
        title: "已上传"+this.data.urls.length+"/"+this.data.images.length,
        mask: true
      });
      let uploadTasks = [];
      for (var i = 0; i < tempFilePath.length; i++) {
        uploadTasks.push(that.uploadFile(tempFilePath[i]));
      }
      Promise.all(uploadTasks).then((results) => {
        wx.hideLoading();
      }).catch((error) => {
        console.error('Error uploading images:', error);
      });
    }
  });
},
uploadFile(filePath) {
  var that=this
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: "https://api.superbed.cn/upload",
      name: 'file',
      filePath: filePath,
      formData: {
        token: 'xxxxxxxxxx',
        src: filePath,
      },
      success: function (res) {
        console.log('Uploaded:', JSON.parse(res.data).url);
        that.data.urls.push(JSON.parse(res.data).url);
        resolve();
      },
      fail: function (error) {
        console.error('Upload failed:', error);
        reject(error);
      }
    });
  });
},
  submit() {
    if(this.data.images.length==this.data.urls.length)
    {
      console.log(this.data.name1)
      var name=this.data.name1
      var time=this.data.time1
      var urls=this.data.urls
      var details=this.data.details
      console.log(this.data.urls)
      if(!name)
      {
        wx.showToast({
          title: '不要为空喔',
          icon: 'error',
          duration: 2000
        })
        return
      }
      wx.request({
        url: app.globalData.url+'/add-word',
        method:"POST",
        data:{
          title:name,
          image:JSON.stringify(urls),
          text:details
        },
        success: res => {
          if(res.data.success)
          {
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 2000
          })
          wx.reLaunch({
            url: '/pages/home/home'
        });
        }
        },
        fail:fail=>{
          console.log(fail)
        },
      })
    }else{
      wx.showToast({
        title: '请等待图片上传完成',
        icon: 'error',
        duration: 2000
      })
      return
    }
    
  },

onInput: function (e) {
  console.log(e.detail.value)
    this.setData({
      name1: e.detail.value
    })
  },
  bindTextAreaBlur: function(e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      details: e.detail.value
    });
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})